package com.mezz.bones.im.logic.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.mezz.bones.framework.base.domain.enums.ResponseCode;
import com.mezz.bones.framework.base.exception.BaseException;
import com.mezz.bones.im.common.constants.MessageConstants;
import com.mezz.bones.im.common.enums.UserTypeEnum;
import com.mezz.bones.im.common.domain.user.ImUserBaseInfo;
import com.mezz.bones.im.logic.domain.entity.ChatUser;
import com.mezz.bones.im.logic.domain.request.ApiUserDeleteRequest;
import com.mezz.bones.im.logic.domain.request.ApiUserImportRequest;
import com.mezz.bones.im.logic.domain.request.ApiUserUpdateRequest;
import com.mezz.bones.im.logic.repository.IChatUserRepository;
import com.mezz.bones.im.logic.service.IChatUserService;
import jakarta.validation.Valid;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.Date;
import java.util.List;

@Service
public class ChatUserServiceImpl implements IChatUserService, CommandLineRunner {

    @Resource
    private IChatUserRepository chatUserRepository;

    @Override
    public void run(String... args) throws Exception {

        //初始化平台主体账号信息
        ChatUser platform = chatUserRepository.getByUserId(MessageConstants.PLATFORM_SUBJECT_CHAT_USER_ID);
        if(platform == null){
            platform = new ChatUser();
            platform.setUserId(MessageConstants.PLATFORM_SUBJECT_CHAT_USER_ID);
            //可读取配置信息
            platform.setNickname("平台客服");
            platform.setAvatar("https://default.png");
            platform.setUserType(UserTypeEnum.SAAS.getCode());
            platform.setCreateTime(new Date());
            platform.setUpdateTime(new Date());
            chatUserRepository.save(platform);
        }

    }

    @Override
    public void removeByUserId(String userId){

        LambdaQueryWrapper<ChatUser> del = new LambdaQueryWrapper<>();
        del.eq(ChatUser::getUserId,userId);
        chatUserRepository.remove(del);
    }

    @Override
    public ChatUser getByUserId(String id) {
        return chatUserRepository.getByUserId(id);
    }

    @Override
    public ImUserBaseInfo getUserBaseInfo(String userId) {

        ChatUser chatUser = chatUserRepository.getByUserId(userId);
        if(chatUser == null){
            return null;
        }

        ImUserBaseInfo res = new ImUserBaseInfo();
        res.setUserId(chatUser.getUserId());
        res.setUserType(chatUser.getUserType());
        res.setNickname(chatUser.getNickname());
        res.setAvatar(chatUser.getAvatar());
        return res;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void importUser(@Valid ApiUserImportRequest request) {

        //查询ID是否已存在
        ChatUser exists = chatUserRepository.getByUserId(request.getUserId());
        if(exists != null){
            throw new BaseException(ResponseCode.DATA_NOT_FOUND,"用户ID已存在!");
        }

        //保存用户信息
        ChatUser user = new ChatUser();
        user.setUserId(request.getUserId());
        user.setUserType(request.getUserType());
        user.setAvatar(request.getAvatar());
        user.setNickname(request.getNickname());
        user.setProfile(request.getProfile());
        Date now = new Date();
        user.setCreateTime(now);
        user.setUpdateTime(now);
        chatUserRepository.save(user);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateUser(ApiUserUpdateRequest request) {

        //查询ID是否已存在
        ChatUser exists = chatUserRepository.getByUserId(request.getUserId());
        if(exists == null){
            throw new BaseException(ResponseCode.DATA_NOT_FOUND,"用户信息不存在!");
        }

        //保存用户信息
        ChatUser user = new ChatUser();
        user.setId(exists.getId());
        user.setAvatar(request.getAvatar());
        user.setNickname(request.getNickname());
        user.setProfile(request.getProfile());
        Date now = new Date();
        user.setUpdateTime(now);
        chatUserRepository.updateById(user);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteUser(ApiUserDeleteRequest request) {

        //查询ID是否已存在
        chatUserRepository.removeByUserId(request.getUserId());
    }

    @Override
    public List<ChatUser> getByUserIds(Collection<String> userIds) {

        return chatUserRepository.getByUserIds(userIds);
    }

}
